import { __private, _decorator, assetManager, Component, Input, input, instantiate, Label, loader, Node, Prefab, resources, Sprite, SpriteFrame, TERRAIN_HEIGHT_BASE, KeyCode } from 'cc';
import { camera } from '../canvas/camera/camera';
const { ccclass, property } = _decorator;

let roleMap = {
    11: {
        name: '雷班纳',
        url: 'img/dialog/player/player_01/spriteFrame',
        tag:'smile'   
    },
    12:{
        name:'雷班纳',
        url:''
    },
    21: {
        name: '三杉',
        url: 'img/dialog/NPC/SanShan_2001/spriteFrame',
        tag: 'nomal'
    },
    22:{
        name:'三杉',
        url:'img/dialog/NPC/SanShan_2002/spriteFrame',
        tag:'surprise'
    }

}

@ccclass('dialog')

export class dialog extends Component {
    private textDataArr = [];
    private textIndex: number = -1;

    private dialog_pictrue = null;
    private Label_name = null;
    private Label_text = null;
    private nowText = null;
    private textEnd = true;
    private titleTime = 0;

    start() {
        console.log("dialog start")
        //关闭player移动，并关闭按钮界面按钮
        camera.tank_player_move = false;
        camera.button_click_and_show = false;
        //如果...点击对话框事件为true，则注册dialog鼠标点击事件
        input.on(Input.EventType.MOUSE_DOWN, this.onMouseDown, this);

        //蓝宝：你获取到的是节点，string是label组件的属性
        this.Label_name = this.node.getChildByName("Label_name")
        this.Label_text = this.node.getChildByName("Label_text")

        this.init([
            { role: 11, content: '喂！老哥，在这里干嘛呢？' },
            { role: 21, content: '毛头小子~？你在哪里搞到的战车？' },
            { role: 21, content: '等等...你那个战车...' },
            { role: 21, content: '难道是...' },
            { role: 11, content: '有什么特别的地方吗？'},
            { role: 21, content: '(这小孩什么来头...怎么会有...'},
            { role: 21, content: '小子，你认识玛利亚吗?'},
            { role: 11, content: '啊~那是我妈妈，你认识她吗？'},
            { role: 21, content: '(妈妈吗...'},
            { role: 21, content: '玛丽长官...你妈妈现在还活着吗？'},
            { role: 12, content: '不...在我出生时她就不在了...'},
            { role: 22, content: '......'},
            { role: 22, content: '......'},
            { role: 21, content: '小子，你过来一趟，你妈妈为你留下了一份礼物，要我送给你...'},
            
        ])
    }

    onMouseDown(){
        //展示下一条信息
        this.nextTextData();
    }


    destroyed() {
        //解绑dialog的鼠标点击事件
        input.off(Input.EventType.MOUSE_DOWN, this.onMouseDown, this);
        console.log("dialog destroyed")
    }

    init(textDataArr) {


        this.textIndex = -1;
        this.textDataArr = textDataArr;
        //控制对话框显示
        // this.node.active = false;
        this.node.active = true;
        this.nextTextData();
    }
    nextTextData() {
        //在没有播放完当前内容时，不会进行下一步对话
        if(!this.textEnd){
            return;
        }

        if (++this.textIndex < this.textDataArr.length) {
            this.setTextData(this.textDataArr[this.textIndex])
        } else {
            this.closeDialog();
        }
    }
    closeDialog() {
        //开启界面按钮功能
        camera.button_click_and_show = true;
        //关闭对话框
        this.node.active = false
        //销毁dialog，解绑鼠标点击事件
        this.destroyed()
    }
    setTextData(textData) {
        //如果结束，下面就不执行，否则textEnd依然为false
        if(!this.textEnd){
            return
        }
        this.textEnd = false

        //string赋值
        this.Label_name.getComponent(Label).string = roleMap[textData.role].name
        // this.Label_text.getComponent(Label).string = textData.content;
        this.Label_text.getComponent(Label).string = "";
        this.nowText = textData.content;

        //动态资源加载
        this.dialog_pictrue = this.node.getChildByName("Sprite_head")
        resources.load(roleMap[textData.role].url, SpriteFrame, (err, spriteFrame) => {
            this.dialog_pictrue.getComponent(Sprite).spriteFrame = spriteFrame;
        });
    }

    update(deltaTime: number) {
        //如果结束，下面就不执行
        if(!this.nowText){
            return
        }
        this.titleTime += deltaTime

        if(this.titleTime >= 0.1){
            //如果现在的text的长度小于总
            if( this.Label_text.getComponent(Label).string.length < this.nowText.length){
                this.Label_text.getComponent(Label).string = this.nowText.slice(0,this.Label_text.getComponent(Label).string.length + 1)
            }else{
                this.textEnd = true;
                this.nowText = null;
            }
        }
    }
}

